package com.athuiju.oss.controller;


import com.athuiju.commonutils.R;
import com.athuiju.oss.service.OssService;
import com.athuiju.oss.utils.FileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ResourceLoader;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.*;

import java.util.*;


@Api(value="上传osscontroller",tags={"上传操作接口"})
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {


    @Value("${web.upload-path}")

    private String path;
    @Autowired
    private OssService ossService;
    @Autowired
    ResourceLoader resourceLoader;

    private static Logger log = LoggerFactory.getLogger(OssController.class);


    @PostMapping
    public R uploadOssFile(MultipartFile file){
       String url= ossService.uploadFileAvatar(file);
       return R.ok().data("url",url);
    }
    //删除头像
    @DeleteMapping("delOssFile/{filepath}")
    public  R delOssFile(@PathVariable("filepath")String filepath){
        filepath=filepath.replaceAll("_","/");//把传过来路径中的下划线_替换成斜杠
        //System.out.println("---------------:::"+filepath);
        Boolean aBoolean=ossService.deleteFileAvatar(filepath);
        if (aBoolean){
            return  R.ok();

        }else {
            return R.error();
        }
    }

    /**
     * 上传图片
     * @return
     * */
    @ApiOperation(value = "上传接口controller")
    @PostMapping("fileUpload")
    public R upload(@RequestParam("fileName")
            MultipartFile file, Map<String, Object> map, HttpServletRequest request) throws IOException {
        StringBuffer requestUrl = request.getRequestURL();
        int index = requestUrl.lastIndexOf("/");
        String requesturl = requestUrl.substring(0, index);
        System.out.println(requesturl);
        // 要上传的目标文件存放路径
        // String localPath = "D:\\wangye\\trytext";
        String localPath=path;
        String filename=file.getOriginalFilename();
        String uuname = UUID.randomUUID().toString().replaceAll("‐","");
        Thumbnails.of(file.getInputStream()).scale(0.8f).outputFormat("jpg").outputQuality(0.5).toFile(path+uuname);
        filename=uuname+filename;
        String datepath = new DateTime().toString("yyyy/MM/dd");
        localPath=localPath+"/"+datepath;
        //filename=datepath+"/"+filenam
        if (FileUtils.upload(file,localPath, filename)){
            String filepath=requesturl+"/show?fileName="+datepath+"/"+filename;
            return R.ok().data("url",filepath);
        }else {
            return R.error();
        }
        }
/**
 * 显示单张图片
 * @return
 * */
    @ApiOperation(value = "显示单张图片接口")
    @GetMapping("show")
    public ResponseEntity showPhotos(String fileName) {
        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            return ResponseEntity.ok(resourceLoader.getResource("file:" + path + "\\" + fileName));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }


    }



